<template>
	<view>
		<my-search @click="gotoSearch"></my-search>
		<view class="scroll-container">
			<scroll-view scroll-y="true" :style="{ height: wh + 'px'}" class="left">
				<block v-for="(item,index) in cateList" :key="index">
					<view :class="['left-scroll-view-item',index==active?'active':'']" @click="activeChanged(index)">
						{{item.cat_name}}
					</view>
				</block>
			</scroll-view>
			<scroll-view scroll-y="true" :style="{ height: wh + 'px'}" class="right" :scroll-top="scrollTop">
				<view class="cate-lv2" v-for="(item,index) in cateLevel2" :key="index">
					<view class="cate-lv2-title">/{{item.cat_name}}/</view>
					<view class="cate-lv3-list">
						<view class="cate-lv3-item" v-for="(item2,index2) in item.children" :key="index2"
							@click="gotoGoodslist(item2)">
							<image :src="item2.cat_icon" style="width: 130rpx;" mode="widthFix"></image>
							<view>{{item2.cat_name}}</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 窗口的高度
				wh: 0,
				cateList: [],
				active: 0,
				cateLevel2: [],
				scrollTop: 0
			};
		},
		onLoad() {
			//api获取设备情况的高度
			const wh = uni.getSystemInfoSync()
			this.wh = wh.windowHeight - 50
			this.getCateList()
		},
		methods: {
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('api/public/v1/categories')
				// console.log(res)
				if (res.meta.status !== 200) {
					return uni.$showMsg()
				}
				this.cateList = res.message
				this.activeChanged(0)
				uni.$showMsg('获取分类列表成功')
			},
			activeChanged(index) {
				this.active = index
				this.cateLevel2 = this.cateList[index].children
				// 返回顶部的值不能相同
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			gotoGoodslist(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
				})
			},
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
	.scroll-container {
		display: flex;

		.left {
			width: 120px;

			.left-scroll-view-item {
				background-color: #f7f7f7;
				line-height: 60px;
				text-align: center;
				font-size: 12px;

				&.active {
					position: relative;
					background-color: #E4E7ED;
					color: #DD524D;

					&::before {
						position: absolute;
						left: 0;
						content: '';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #DD524D;
						top: 50%;
						transform: translate(0, -50%);
					}
				}
			}
		}

		.cate-lv2-title {
			text-align: center;
			font-size: 12px;
			font-weight: 600;
			padding: 15px 0;
		}

		.cate-lv3-list {
			display: flex;
			font-size: 12px;
			flex-wrap: wrap;

			.cate-lv3-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				width: 33.33%;
			}
		}
	}
</style>
